package com.ruoyi.zsyh.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Map;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.zsyh.domain.ZsyhAgentCooperateRel;
import com.ruoyi.zsyh.mapper.ZsyhAgentCooperateMapper;
import com.ruoyi.zsyh.domain.ZsyhAgentCooperate;
import com.ruoyi.zsyh.service.IZsyhAgentCooperateService;
import com.ruoyi.common.core.text.Convert;

/**
 * 大区会长产品合作Service业务层处理
 * 
 * @author yuexl
 * @date 2024-10-15
 */
@Service
public class ZsyhAgentCooperateServiceImpl implements IZsyhAgentCooperateService 
{
    @Autowired
    private ZsyhAgentCooperateMapper zsyhAgentCooperateMapper;

    /**
     * 查询大区会长产品合作
     * 
     * @param id 大区会长产品合作ID
     * @return 大区会长产品合作
     */
    @Override
    public ZsyhAgentCooperate selectZsyhAgentCooperateById(String id)
    {
        return zsyhAgentCooperateMapper.selectZsyhAgentCooperateById(id);
    }

    /**
     * 查询大区会长产品合作列表
     * 
     * @param zsyhAgentCooperate 大区会长产品合作
     * @return 大区会长产品合作
     */
    @Override
    public List<ZsyhAgentCooperate> selectZsyhAgentCooperateList(ZsyhAgentCooperate zsyhAgentCooperate)
    {
        return zsyhAgentCooperateMapper.selectZsyhAgentCooperateList(zsyhAgentCooperate);
    }

    /**
     * 新增大区会长产品合作
     * 
     * @param zsyhAgentCooperate 大区会长产品合作
     * @return 结果
     */
    @Transactional
    @Override
    public int insertZsyhAgentCooperate(ZsyhAgentCooperate zsyhAgentCooperate)
    {
        zsyhAgentCooperate.setCreateTime(DateUtils.getNowDate());
        int rows = zsyhAgentCooperateMapper.insertZsyhAgentCooperate(zsyhAgentCooperate);
        insertZsyhAgentCooperateRel(zsyhAgentCooperate);
        return rows;
    }

    public int insertZsyhAgentCooperateApplet(ZsyhAgentCooperate zsyhAgentCooperate)
    {
        zsyhAgentCooperate.setCreateTime(DateUtils.getNowDate());
        int rows = zsyhAgentCooperateMapper.insertZsyhAgentCooperate(zsyhAgentCooperate);
        return rows;
    }
    /**
     * 修改大区会长产品合作
     * 
     * @param zsyhAgentCooperate 大区会长产品合作
     * @return 结果
     */
    @Transactional
    @Override
    public int updateZsyhAgentCooperate(ZsyhAgentCooperate zsyhAgentCooperate)
    {
        zsyhAgentCooperate.setUpdateTime(DateUtils.getNowDate());
        zsyhAgentCooperateMapper.deleteZsyhAgentCooperateRelByCooperateId(zsyhAgentCooperate.getId());
        insertZsyhAgentCooperateRel(zsyhAgentCooperate);
        return zsyhAgentCooperateMapper.updateZsyhAgentCooperate(zsyhAgentCooperate);
    }

    @Override
    public int updateZsyhAgentCooperateEasy(ZsyhAgentCooperate zsyhAgentCooperate) {
        zsyhAgentCooperate.setUpdateTime(DateUtils.getNowDate());
        return zsyhAgentCooperateMapper.updateZsyhAgentCooperate(zsyhAgentCooperate);
    }

    /**
     * 删除大区会长产品合作对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteZsyhAgentCooperateByIds(String ids)
    {
        zsyhAgentCooperateMapper.deleteZsyhAgentCooperateRelByCooperateIds(Convert.toStrArray(ids));
        return zsyhAgentCooperateMapper.deleteZsyhAgentCooperateByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除大区会长产品合作信息
     * 
     * @param id 大区会长产品合作ID
     * @return 结果
     */
    @Override
    public int deleteZsyhAgentCooperateById(String id)
    {
        zsyhAgentCooperateMapper.deleteZsyhAgentCooperateRelByCooperateId(id);
        return zsyhAgentCooperateMapper.deleteZsyhAgentCooperateById(id);
    }

    /**
     * 新增大区会长产品合作区域信息
     * 
     * @param zsyhAgentCooperate 大区会长产品合作对象
     */
    public void insertZsyhAgentCooperateRel(ZsyhAgentCooperate zsyhAgentCooperate)
    {
        List<ZsyhAgentCooperateRel> zsyhAgentCooperateRelList = zsyhAgentCooperate.getZsyhAgentCooperateRelList();
        String id = zsyhAgentCooperate.getId();
        if (StringUtils.isNotNull(zsyhAgentCooperateRelList))
        {
            List<ZsyhAgentCooperateRel> list = new ArrayList<ZsyhAgentCooperateRel>();
            for (ZsyhAgentCooperateRel zsyhAgentCooperateRel : zsyhAgentCooperateRelList)
            {
                zsyhAgentCooperateRel.setCooperateId(id);
                list.add(zsyhAgentCooperateRel);
            }
            if (list.size() > 0)
            {
                zsyhAgentCooperateMapper.batchZsyhAgentCooperateRel(list);
            }
        }
    }

    public Map getAgentTargetAll(Map map){
        return zsyhAgentCooperateMapper.getAgentTargetAll(map);
    }

    public List<Map> getCooperateAreaList(Map map){
        return zsyhAgentCooperateMapper.getCooperateAreaList(map);
    }

    public List<Map> getNotCooperateAreaList(Map map){
        return zsyhAgentCooperateMapper.getNotCooperateAreaList(map);
    }


    public List<Map> selectAgentCooperateByAgentId(Map map){
        return zsyhAgentCooperateMapper.selectAgentCooperateByAgentId(map);
    }
}
